package com.zlt.appparking.config;


import com.zlt.appparking.shiro.filter.ShiroAuthcFilter;
import com.zlt.appparking.shiro.realm.AppUserRealm;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {


    @Autowired
    private AppUserRealm appUserRealm;


    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(appUserRealm);
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        DefaultSubjectDAO defaultSubjectDAO = new DefaultSubjectDAO();
        defaultSubjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        defaultWebSecurityManager.setSubjectDAO(defaultSubjectDAO);
        return defaultWebSecurityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("authc",new ShiroAuthcFilter());
        shiroFilterFactoryBean.setFilters(filterMap);
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();
//        filterChainDefinitionMap.put("/user/login","anon"); //设置不需要拦截的路径
//        filterChainDefinitionMap.put("/swagger","anon"); //设置不需要拦截的路径
//        filterChainDefinitionMap.put("/swagger/**","anon"); //设置不需要拦截的路径
//        filterChainDefinitionMap.put("/user/reg","anon"); //设置不需要拦截的路径
        filterChainDefinitionMap.put("/license/**","anon"); //设置不需要拦截的路径
        filterChainDefinitionMap.put("/map/**","anon");
        filterChainDefinitionMap.put("/member/**","anon");
//        filterChainDefinitionMap.put("/user/kaptcha","anon");
//        filterChainDefinitionMap.put("/user/**","authc");//设置需要拦截的路径
//        filterChainDefinitionMap.put("/adminUser/kaptcha","anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        //设置需要拦截的路径

        filterChainDefinitionMap.put("/app_user/login","anon"); //设置不需要拦截的路径
        filterChainDefinitionMap.put("/app_user/kaptcha","anon");
        filterChainDefinitionMap.put("/app_user/reg","anon");
        filterChainDefinitionMap.put("/app_user/regPhoneCode","anon");
        filterChainDefinitionMap.put("/app_user/loginPhoneCode","anon");
        filterChainDefinitionMap.put("/app_user/forgetPhoneCode","anon");
        filterChainDefinitionMap.put("/app_user/updateAppUserPassword","anon");
        filterChainDefinitionMap.put("/app_user/**","authc");//设置需要拦截的路径
        filterChainDefinitionMap.put("/feedback/**","authc");
        filterChainDefinitionMap.put("/appointments/**","authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager defaultWebSecurityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
